/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.UserInfoPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  UserInfoPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class UserInfoSqlProvider {
	public static final String TABLE_NAME = "user_info";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param userInfo
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserInfos(final UserInfoPo userInfo) {
		return new CustomerSQL() {{
			SELECT("promo_code,address,birthday,create_time,creator_id,email,gender,id,ip_last_login,ip_register,member_level_id,member_level_name,member_score,mobile,password,realname,salt,telephone,update_time,updater_id,username,head,status,type ");
			FROM(TABLE_NAME);
			
			if (StringUtils.isNotBlank(userInfo.getAddress())) {
				WHERE("address=#{address}");
			}
			if (null != userInfo.getBirthday()) {
				WHERE("birthday=#{birthday}");
			}
			if (null != userInfo.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userInfo.getEmail())) {
				WHERE("email=#{email}");
			}
			if (null != userInfo.getGender()) {
				WHERE("gender=#{gender}");
			}
			if (StringUtils.isNotBlank(userInfo.getIpLastLogin())) {
				WHERE("ip_last_login=#{ipLastLogin}");
			}
			if (StringUtils.isNotBlank(userInfo.getIpRegister())) {
				WHERE("ip_register=#{ipRegister}");
			}
			if (null != userInfo.getMemberLevelId()) {
				WHERE("member_level_id=#{memberLevelId}");
			}
			if (StringUtils.isNotBlank(userInfo.getMemberLevelName())) {
				WHERE("member_level_name=#{memberLevelName}");
			}
			if (null != userInfo.getMemberScore()) {
				WHERE("member_score=#{memberScore}");
			}
			if (StringUtils.isNotBlank(userInfo.getMobile())) {
				WHERE("mobile LIKE CONCAT(#{mobile},'%' ) ");
			}
			if (StringUtils.isNotBlank(userInfo.getPassword())) {
				WHERE("password=#{password}");
			}
			if (StringUtils.isNotBlank(userInfo.getRealname())) {
				WHERE("realname LIKE CONCAT('%',#{realname},'%' ) ");
			}
			if (StringUtils.isNotBlank(userInfo.getSalt())) {
				WHERE("salt=#{salt}");
			}
			if (StringUtils.isNotBlank(userInfo.getTelephone())) {
				WHERE("telephone=#{telephone}");
			}
			if (null != userInfo.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (StringUtils.isNotBlank(userInfo.getUsername())) {
				WHERE("username LIKE CONCAT('%',#{username},'%' ) ");
			}
			if (null != userInfo.getPromoterId()) {
				WHERE("promoter_id=#{promoterId}");
			}
			if (StringUtils.isNoneBlank(userInfo.getPromoCode())) {
				WHERE("promo_code=#{promoCode}");
			}
			if (StringUtils.isNoneBlank(userInfo.getOpenid())) {
				WHERE("openid=#{openid}");
			}
			if (null != userInfo.getStatus()) {
				WHERE("status=#{status}");
			}
			if (null != userInfo.getType()) {
				WHERE("type=#{type}");
			}
			if(StringUtils.isNotBlank(userInfo.getSortField())) {
				ORDER_BY(userInfo.getSortField() + (StringUtils.isNotBlank(userInfo.getSortMethod()) ? "desc" : userInfo.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != userInfo.getStart() && null != userInfo.getSize()) {
				LIMIT(userInfo.getStart(), userInfo.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param userInfo
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserInfoCount(final UserInfoPo userInfo) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (StringUtils.isNotBlank(userInfo.getAddress())) {
				WHERE("address=#{address}");
			}
			if (null != userInfo.getBirthday()) {
				WHERE("birthday=#{birthday}");
			}
			if (null != userInfo.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userInfo.getEmail())) {
				WHERE("email=#{email}");
			}
			if (null != userInfo.getGender()) {
				WHERE("gender=#{gender}");
			}
			if (StringUtils.isNotBlank(userInfo.getIpLastLogin())) {
				WHERE("ip_last_login=#{ipLastLogin}");
			}
			if (StringUtils.isNotBlank(userInfo.getIpRegister())) {
				WHERE("ip_register=#{ipRegister}");
			}
			if (null != userInfo.getMemberLevelId()) {
				WHERE("member_level_id=#{memberLevelId}");
			}
			if (StringUtils.isNotBlank(userInfo.getMemberLevelName())) {
				WHERE("member_level_name=#{memberLevelName}");
			}
			if (null != userInfo.getMemberScore()) {
				WHERE("member_score=#{memberScore}");
			}
			if (StringUtils.isNotBlank(userInfo.getMobile())) {
				WHERE("mobile LIKE CONCAT(#{mobile},'%' ) ");
			}
			if (StringUtils.isNotBlank(userInfo.getPassword())) {
				WHERE("password=#{password}");
			}
			if (StringUtils.isNotBlank(userInfo.getRealname())) {
				WHERE("realname LIKE CONCAT('%',#{realname},'%' ) ");
			}
			if (StringUtils.isNotBlank(userInfo.getSalt())) {
				WHERE("salt=#{salt}");
			}
			if (StringUtils.isNotBlank(userInfo.getTelephone())) {
				WHERE("telephone=#{telephone}");
			}
			if (null != userInfo.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (StringUtils.isNotBlank(userInfo.getUsername())) {
				WHERE("username LIKE CONCAT('%',#{username},'%' ) ");
			}
			if (null != userInfo.getPromoterId()) {
				WHERE("promoter_id=#{promoterId}");
			}
			if (StringUtils.isNoneBlank(userInfo.getPromoCode())) {
				WHERE("promo_code=#{promoCode}");
			}
			if (StringUtils.isNoneBlank(userInfo.getOpenid())) {
				WHERE("openid=#{openid}");
			}
			if (null != userInfo.getStatus()) {
				WHERE("status=#{status}");
			}
			if (null != userInfo.getType()) {
				WHERE("type=#{type}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserInfoById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param userInfo
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addUserInfo(final UserInfoPo userInfo) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (StringUtils.isNotBlank(userInfo.getAddress())) {
				VALUES("address","#{address}");
			}
			if (null != userInfo.getBirthday()) {
				VALUES("birthday","#{birthday}");
			}
			if (null != userInfo.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(userInfo.getEmail())) {
				VALUES("email","#{email}");
			}
			if (null != userInfo.getGender()) {
				VALUES("gender","#{gender}");
			}
			if (StringUtils.isNotBlank(userInfo.getIpLastLogin())) {
				VALUES("ip_last_login","#{ipLastLogin}");
			}
			if (StringUtils.isNotBlank(userInfo.getIpRegister())) {
				VALUES("ip_register","#{ipRegister}");
			}
			if (null != userInfo.getMemberLevelId()) {
				VALUES("member_level_id","#{memberLevelId}");
			}
			if (StringUtils.isNotBlank(userInfo.getMemberLevelName())) {
				VALUES("member_level_name","#{memberLevelName}");
			}
			if (null != userInfo.getMemberScore()) {
				VALUES("member_score","#{memberScore}");
			}
			if (StringUtils.isNotBlank(userInfo.getMobile())) {
				VALUES("mobile","#{mobile}");
			}
			if (StringUtils.isNotBlank(userInfo.getPassword())) {
				VALUES("password","#{password}");
			}
			if (StringUtils.isNotBlank(userInfo.getRealname())) {
				VALUES("realname","#{realname}");
			}
			if (StringUtils.isNotBlank(userInfo.getSalt())) {
				VALUES("salt","#{salt}");
			}
			if (StringUtils.isNotBlank(userInfo.getTelephone())) {
				VALUES("telephone","#{telephone}");
			}
			if (StringUtils.isNotBlank(userInfo.getOpenid())) {
				VALUES("openid","#{openid}");
			}
			if (null != userInfo.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
			if (StringUtils.isNotBlank(userInfo.getUsername())) {
				VALUES("username","#{username}");
			}
			if (null != userInfo.getPromoterId()) {
				VALUES("promoter_id","#{promoterId}");
			}
			if (StringUtils.isNoneBlank(userInfo.getPromoCode())) {
				VALUES("promo_code","#{promoCode}");
			}
			if (StringUtils.isNotBlank(userInfo.getPromoterName())) {
				VALUES("promoter_name","#{promoterName}");
			}
			if (StringUtils.isNotBlank(userInfo.getHead())) {
				VALUES("head","#{head}");
			}
			if (null != userInfo.getStatus()) {
				VALUES("status","#{status}");
			}
			if (null != userInfo.getType()) {
				VALUES("type","#{type}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param userInfo
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateUserInfo(final UserInfoPo userInfo) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (StringUtils.isNotBlank(userInfo.getAddress())) {
				SET("address=#{address}");
			}
			if (null != userInfo.getBirthday()) {
				SET("birthday=#{birthday}");
			}
			if (null != userInfo.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userInfo.getEmail())) {
				SET("email=#{email}");
			}
			if (null != userInfo.getGender()) {
				SET("gender=#{gender}");
			}
			if (null != userInfo.getOpenid()) {
				SET("openid=#{openid}");
			}
			if (StringUtils.isNotBlank(userInfo.getIpLastLogin())) {
				SET("ip_last_login=#{ipLastLogin}");
			}
			if (StringUtils.isNotBlank(userInfo.getIpRegister())) {
				SET("ip_register=#{ipRegister}");
			}
			if (null != userInfo.getMemberLevelId()) {
				SET("member_level_id=#{memberLevelId}");
			}
			if (StringUtils.isNotBlank(userInfo.getMemberLevelName())) {
				SET("member_level_name=#{memberLevelName}");
			}
			if (null != userInfo.getMemberScore()) {
				SET("member_score=#{memberScore}");
			}
			if (StringUtils.isNotBlank(userInfo.getMobile())) {
				SET("mobile=#{mobile}");
			}
			if (StringUtils.isNotBlank(userInfo.getPassword())) {
				SET("password=#{password}");
			}
			if (StringUtils.isNotBlank(userInfo.getRealname())) {
				SET("realname=#{realname}");
			}
			if (StringUtils.isNotBlank(userInfo.getSalt())) {
				SET("salt=#{salt}");
			}
			if (StringUtils.isNotBlank(userInfo.getTelephone())) {
				SET("telephone=#{telephone}");
			}
			if (null != userInfo.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			if (StringUtils.isNotBlank(userInfo.getUsername())) {
				SET("username=#{username}");
			}
			if (StringUtils.isNotBlank(userInfo.getHead())) {
				SET("head=#{head}");
			}
			if (null != userInfo.getStatus()) {
				SET("status=#{status}");
			}
			if (null != userInfo.getType()) {
				SET("type=#{type}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delUserInfo(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
